﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Resources;
using System.Globalization;


namespace QLCHQA
{
    public partial class Ban_hang : Form
    {
        string id;
        long ttien = 0;
        string idhd = "";
        public Ban_hang()
        {
            InitializeComponent();
        }

        private void Ban_hang_Load(object sender, EventArgs e)
        {

        }

        public void chuyendl(string id)
        {
            this.id = id;
        }


        private void bttXoa_Click(object sender, EventArgs e)
        {
            int icountSelectedRow = dataGridView1.SelectedColumns.Count + 1;
            if (icountSelectedRow == 0)
                MessageBox.Show("Bạn hãy chọn dòng cần xoá!");
            else foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    if (!row.IsNewRow)
                    {
                        ttien = ttien - int.Parse(row.Cells[8].Value.ToString());
                        txtTongtien.Text = ttien + "";
                        dataGridView1.Rows.Remove(row);                    
                        for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                        {
                            dataGridView1["stt", i].Equals(i + "");
                            
                            
                        }

                    }
            tbsl.Clear();
            tbmahang.Clear();
            cbncc.DataSource = null;
            cbms.DataSource = null;
            tbmahang.Focus();
        

        }



        private void bttclose_Click(object sender, EventArgs e)
        {
            this.Hide();
            Lua_chon cs = new Lua_chon();
            cs.chuyendl(id);
            cs.Show();
            
        }

        private void bttTT_Click(object sender, EventArgs e)
        {
            
            string date = DateTime.Now.ToString();
          
            //string datetime = month + "/" + date + "/" + year;
            //DateTime dtime = new DateTime(year, month, date);
            string mahang = tbmahang.Text;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            String sql = "select idhd from hdban";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "mhd");
            dt = ds.Tables["mhd"];
            int mhd = (dt.Rows.Count + 1) ;
            string id1 = id.Trim();
            int tt = int.Parse(txtTongtien.Text);      
            SqlCommand add = new SqlCommand(@"insert into hdban values(@idhd,@id,@giatri,@km,@date,@makh)", conn);
            add.Parameters.AddWithValue( "idhd",mhd.ToString());
            add.Parameters.AddWithValue( "id",id1);
            add.Parameters.AddWithValue( "giatri",tt);
            add.Parameters.AddWithValue("km", "");
            add.Parameters.AddWithValue("date", date);
            add.Parameters.AddWithValue("makh", "01");
            conn.Open();
            add.ExecuteNonQuery();
            conn.Close();
            for(int c = 0; c < this.dataGridView1.Rows.Count -1 ; c++)
            {
                string idsp = dataGridView1.Rows[c].Cells[1].Value.ToString();
                string idms = dataGridView1.Rows[c].Cells[3].Value.ToString();
                string size = dataGridView1.Rows[c].Cells[2].Value.ToString();
                string sl = dataGridView1.Rows[c].Cells[7].Value.ToString();
                SqlCommand add2 = new SqlCommand(@"insert into chitietban values(@idhd,@idsp,@idms,@size,@sl)", conn);
                add2.Parameters.AddWithValue("idhd", mhd.ToString());
                add2.Parameters.AddWithValue("idsp", idsp);
                add2.Parameters.AddWithValue("idms", idms);
                add2.Parameters.AddWithValue("size", size);
                add2.Parameters.AddWithValue("sl", sl);
                conn.Open();
                add2.ExecuteNonQuery();
                    conn.Close();
                 sql = "select sl from sanpham where idsp = '"+idsp+"'";
                  da = new SqlDataAdapter(sql, conn);
                    da.Fill(ds, "datasl");
                    dt = ds.Tables["datasl"];
                    DataRow dr = dt.Rows[0];
                    int slc = int.Parse(dr[0].ToString());
                    int slm = slc - int.Parse(sl);
                    sql = "select sl from ctsp where idsp = '" + idsp + "' and ms='" + idms + "' and size ='" + size + "'";
                    SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
                    da1.Fill(ds, "datasl1");
                    DataTable dt1 = ds.Tables["datasl1"];
                    if (dt1.Rows.Count == 0)
                    {
                        MessageBox.Show("Xem lại chi tiết về hàng vừa nhập");
                    }
                    else
                    {
                    int slc1 = int.Parse((dt1.Rows[0])[0].ToString());                 
                        conn.Open();
                        SqlCommand update = new SqlCommand(@"update sanpham set sl = @slm where idsp = @idsp", conn);
                        update.Parameters.AddWithValue("slm", slm);
                        update.Parameters.AddWithValue("idsp", idsp);
                        update.ExecuteNonQuery();
                        update.Clone();
                        conn.Close();

                        int slm1 = slc1 - int.Parse(sl);
                        conn.Open();
                        SqlCommand update2 = new SqlCommand(@"update ctsp set sl = @slm where idsp =@idsp and ms=@idms and size =@size", conn);
                        update2.Parameters.AddWithValue("@slm", slm1);
                        update2.Parameters.AddWithValue("@idsp", idsp);
                        update2.Parameters.AddWithValue("@idms", idms);
                        update2.Parameters.AddWithValue("@size", size);
                        update2.ExecuteNonQuery();
                        update2.Clone();
                        conn.Close();
                    }
            }
            

            idhd = mhd.ToString();
         DialogResult lkResult = MessageBox.Show("Khách hàng có tài khoản thân thiết?","" ,MessageBoxButtons.YesNo);

               if (lkResult == DialogResult.Yes)

               {
                   Thanhtoan ttoan = new Thanhtoan();
                   ttoan.gettt(idhd, id, ttien);
                   ttoan.Show();
                   this.Close();
               }

    if (lkResult == DialogResult.No)
    {
                   Thanhtoan01 tt01 = new Thanhtoan01();
                   tt01.gettt(idhd,id, ttien);
                   tt01.Show();
                   this.Close();
               }
                              
                }

       
       

        private void tbmahang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string msp = tbmahang.Text;
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                string sql = "select DISTINCT size from ctsp where idsp = '" + msp + "'  ";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "sizehang");
                dt = ds.Tables["sizehang"];
                var items1 = new BindingList<KeyValuePair<string, string>>();
                items1.Add(new KeyValuePair<string, string>("0", "Chọn kích cỡ"));

                if (dt.Rows.Count != 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                        items1.Add(new KeyValuePair<string, string>(dt.Rows[i][0].ToString(), dt.Rows[i][0].ToString()));
                    }
                    cbncc.DataSource = items1;
                    cbncc.ValueMember = "Key";
                    cbncc.DisplayMember = "Value";
                    string sql2 = "select DISTINCT ms from ctsp where idsp = '" + msp + "'";
                    SqlDataAdapter da2 = new SqlDataAdapter(sql2, conn);
                    da2.Fill(ds, "mausac");
                    DataTable dt1 = ds.Tables["mausac"];
                    var items = new BindingList<KeyValuePair<string, string>>();
                    items.Add(new KeyValuePair<string, string>("0", "Chọn màu"));

                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        string ms = dt1.Rows[j][0].ToString().Trim();
                        string sql3 = "select tenms from mau where ms = '" + ms + "'  ";
                        SqlDataAdapter da3 = new SqlDataAdapter(sql3, conn);
                        da3.Fill(ds, "tenmausac");
                        DataTable dt2 = ds.Tables["tenmausac"];
                        string tenmau = ms + " | " + dt2.Rows[j][0].ToString();
                        items.Add(new KeyValuePair<string, string>(ms, tenmau));

                    }
                    cbms.DataSource = items;
                    cbms.ValueMember = "Key";
                    cbms.DisplayMember = "Value";

                }             
                else
                {
                    tbsl.Clear();
                    tbmahang.Clear();
                    tbmahang.Focus();
                    MessageBox.Show("Nhập sai mã hàng, nhập lại");
                }
            }

        }

        private void tbsl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    string msp = tbmahang.Text;
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    String sql = "select * from sanpham where idsp = '" + msp + "'  ";
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.Fill(ds, "mathang");
                    dt = ds.Tables["mathang"];

                    if (dt.Rows.Count != 0)
                    {
                        if (tbsl.Text == "")
                            MessageBox.Show("Vui lòng nhập số lượng hàng");
                        else
                        {
                            sql = "select sl from ctsp where idsp = '" + msp + "' and ms='" + cbms + "' and size ='" + cbms + "'";
                            SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
                            da1.Fill(ds, "datasl1");
                            DataTable dt1 = ds.Tables["datasl1"];
                            if (dt1.Rows.Count == 0)
                            {
                                MessageBox.Show("Xem lại chi tiết về hàng vừa nhập");
                            }
                            else
                            {
                                int sl = Convert.ToInt32(tbsl.Text);
                                DataRow dr = dt.Rows[0];
                                long tt = long.Parse(dr[3] + "") * sl;
                                dataGridView1.Rows.Add(dataGridView1.Rows.Count.ToString(), dr[0], cbncc.SelectedValue, cbms.SelectedValue, dr[1], "Chiếc", dr[3], sl, tt);
                                ttien += tt;
                                txtTongtien.Text = ttien + "";

                            }
                        }
                    }
                    

                }
                catch
                   (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                tbsl.Clear();
                tbmahang.Clear();
                cbncc.DataSource = null;
                cbms.DataSource = null;
                tbmahang.Focus();

            }
        }

     

        private void bttTH_Click(object sender, EventArgs e)
        {
            try
            {
                string msp = tbmahang.Text;
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                String sql = "select * from sanpham where idsp = '" + msp + "'  ";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "mathang");
                dt = ds.Tables["mathang"];

                if (dt.Rows.Count != 0)
                {
                    if (tbsl.Text == "")
                        MessageBox.Show("Vui lòng nhập số lượng hàng");
                    else
                    {
                        int sl = Convert.ToInt32(tbsl.Text);
                        DataRow dr = dt.Rows[0];
                        long tt = long.Parse(dr[3] + "") * sl;
                        dataGridView1.Rows.Add(dataGridView1.Rows.Count.ToString(), dr[0], cbncc.SelectedValue, cbms.SelectedValue, dr[1], "Chiếc", dr[3], sl, tt);
                        ttien += tt;
                        txtTongtien.Text = ttien + "";

                    }
                }

            }
            catch
                         (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

            tbsl.Clear();
            tbmahang.Clear();
            cbncc.DataSource = null;
            tbmahang.Focus();

        
        }

        private void cbsize_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        }
}
    

